package br.ucb.mercadojogador.HIB;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.exception.ConstraintViolationException;

import br.ucb.mercadojogador.beans.Presidente;


public class PresidenteHIB {

	public void salvar(Presidente presidente) {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		session.saveOrUpdate(presidente);
		transaction.commit();
		session.close();
	}
	
	public void excluir(Presidente presidente) throws ConstraintViolationException {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		session.delete(presidente);
		transaction.commit();
		session.close();
	}
	
	@SuppressWarnings("unchecked")
	public List<Presidente> listar() {
	Session session = HibernateUtil.getSession();
		try {
			return session.createCriteria(Presidente.class).list();
		} finally {
			session.close();
		}
	}
	
	public Presidente consultar(long idPres) {
	Session session = HibernateUtil.getSession();
		try {
			Presidente presidente = (Presidente) session.get(Presidente.class,idPres);
			return presidente;
		} finally {
			session.close();
		}
	}
}
